package cn.neusoft.controller;


import cn.neusoft.exception.AppException;
import cn.neusoft.po.Menu;
import cn.neusoft.query.MenuQuery;
import cn.neusoft.service.IMenuService;
import cn.neusoft.vo.ResponseObj;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hxj
 * @since 2023-12-19
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    IMenuService service;

    @PostMapping
    public ResponseObj saveOrUpdate(@RequestBody Menu menu){
        boolean b = service.saveOrUpdate(menu);
        return b?ResponseObj.SUCCESS():ResponseObj.ERROR(30001,"保存菜单信息失败");
    }

    @DeleteMapping("/{id}")
    public ResponseObj delete(@PathVariable("id") Integer id){
        boolean b = service.removeById(id);
        return b?ResponseObj.SUCCESS():ResponseObj.ERROR(30002,"删除菜单信息失败");
    }

    @GetMapping("/{id}")
    public ResponseObj getById(@PathVariable("id") Integer id){
        Menu menu = service.getById(id);
        return ResponseObj.SUCCESS(menu);
    }

    @PostMapping("/page")
    public ResponseObj getByPage(@RequestBody MenuQuery query){
        QueryWrapper queryWrapper = new QueryWrapper();
        if (query.getName()!=null){
            queryWrapper.like("name",query.getName());
        }
        Page pageQuery = new Page(query.getCurrent(),query.getSize());
        Page<Menu> page = service.page(pageQuery, queryWrapper);
        return ResponseObj.SUCCESS(page);
    }

    @GetMapping("/all")
    public ResponseObj getAll(){
        List<Menu> list = service.list();
        return ResponseObj.SUCCESS(list);
    }

    @PostMapping("/tree")
    public ResponseObj tree(@RequestBody MenuQuery query){
        List<Menu> list = service.getTree(query);
        return ResponseObj.SUCCESS(list);
    }

    /**
     * 查询一级菜单 或者 通过一级菜单id查询二级菜单
     * @param pid
     * @return
     */
    @GetMapping(value = {"/byParent/{pid}","/byParent"})
    public ResponseObj getByParent(@PathVariable(value = "pid",required = false) Integer pid){
        QueryWrapper queryWrapper = new QueryWrapper();
        if (pid==null){
            queryWrapper.isNull("parent_Id");
        }else{
            queryWrapper.eq("parent_Id",pid);
        }
        List<Menu> list = service.list(queryWrapper);
        return ResponseObj.SUCCESS(list);
    }

    /**
     * 查询角色对应的菜单树
     * @param roleId
     * @return
     */
    @GetMapping("/roleMenu/{roleId}")
    public ResponseObj getRoleMenu(@PathVariable("roleId") Integer roleId){
        List<Menu> roleMenu = service.getRoleMenuTree(roleId);
        return ResponseObj.SUCCESS(roleMenu);
    }


    /**
     * 通过角色id查询该角色拥有的菜单
     * @param roleId
     * @return
     */
    @GetMapping("/roleMenuList/{roleId}")
    public ResponseObj getRoleMenuList(@PathVariable("roleId") Integer roleId){
        List<Menu> roleMenu = service.getRoleMenuList(roleId);
        return ResponseObj.SUCCESS(roleMenu);
    }

    /**
     * 修改菜单状态
     * @param menus
     * @return
     */
    @PostMapping("/changeState")
    public ResponseObj changeState(@RequestBody List<Menu> menus){
        boolean b = service.updateBatchById(menus);
        return b?ResponseObj.SUCCESS():ResponseObj.ERROR(30001,"更新菜单状态失败");
    }

    @GetMapping("/level/{level}")
    public ResponseObj getBylevel(@PathVariable("level") Integer level){
        QueryWrapper queryWrapper = new QueryWrapper();
        if (level==1){
            queryWrapper.isNull("parent_id");
        }else if(level==2){
            queryWrapper.isNotNull("parent_id");
        }
        List<Menu> list = service.list(queryWrapper);
        return ResponseObj.SUCCESS(list);
    }
    
}
